package com.louis.screenadapter_pixel;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class Utils {
    private static Utils utils;

    //参考宽高
    private static final float STAND_WIDTH = 720;
    private static final float STAND_HEIGHT = 1280;

    //屏幕显示宽高
    private int mDisplayWidth;
    private int mDisplayHeight;
    private Utils(Context context) {
        //获取屏幕的宽高
        if (mDisplayWidth == 0 || mDisplayHeight == 0) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if (windowManager != null) {
                DisplayMetrics displayMetrics = new DisplayMetrics();
                windowManager.getDefaultDisplay().getMetrics(displayMetrics);
                if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
                    //宽大于高，横屏
                    mDisplayWidth = displayMetrics.heightPixels;
                    mDisplayHeight = displayMetrics.widthPixels;
                } else {
                    mDisplayWidth = displayMetrics.widthPixels;
                    mDisplayHeight = displayMetrics.heightPixels - getStatusBarHeight(context);
                }
            }
        }
    }

    public int getStatusBarHeight(Context context){
        int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resId > 0) {
            return context.getResources().getDimensionPixelSize(resId);
        }
        return 0;
    }

    public static Utils getInstance(Context context) {
        if (utils == null){
            utils = new Utils(context.getApplicationContext());
        }
        return utils;
    }

    //水平方向缩放比例
    public float getHorizontalScale(){
        return mDisplayWidth/ STAND_WIDTH;
    }

    //垂直方向缩放比例
    public float getVerticalScale(){
        return mDisplayHeight/ STAND_HEIGHT;
    }
}
